import WxRequest from '../lib/mina-request/index'

const request = new WxRequest({
    baseURL: "http://192.168.137.1:8080/api",
    timeout: 15000,
    isLoading: false
})
// 请求拦截器
request.interceptors.request = (config) => {
    const token = wx.getStorageSync('token')
    if (token) {
        config.header['token'] = token
    }
    return config
}
// 响应拦截器
request.interceptors.response = (res) => {
    const {
        isSuccess,
        data,
        statusCode
    } = res
    if (!isSuccess) {
        wx.showToast({
            title: '网络异常',
            icon: 'error',
            duration: 3000
        });
        return Promise.reject(res)
    } else {
        switch (statusCode) {
            case 200:
                return data
            case 401:
              wx.clearStorageSync()
                wx.showModal({
                    title: '提示',
                    content: '请登录',
                    showCancel: false, // 不显示取消按钮
                    // 点击确定
                    success: (res) => {
                        wx.navigateTo({
                            url: '/pages/LoginPage/LoginPage',
                        })
                    }
                })
                return Promise.reject(res)
            default:
                wx.showToast({
                    title: '程序出现异常，请联系客服或稍后重试！',
                })
                return Promise.reject(res)
        }
    }
}

export default request